package net.xdclass.component;

import net.xdclass.enums.ProductOrderPayTypeEnum;
import net.xdclass.vo.PayInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: wangzhan
 * @Description: TODO
 * @DateTime: 2024/9/16 22:21
 **/
@Component
public class PayFactory {
    @Autowired
    private AliPayStrategy aliPayStrategy;
    @Autowired
    private WechatPayStrategy wechatPayStrategy;

    //创建支付，采用简单工厂模式
    public String pay(PayInfoVO payInfoVO){
        String payType = payInfoVO.getPayType();
        if(payType.equals(ProductOrderPayTypeEnum.WECHAT_PAY.name())){
            //微信支付
            PayStrategyContext payStrategyContext = new PayStrategyContext(wechatPayStrategy);
            return payStrategyContext.executeUnifiedOrder(payInfoVO);
        }else if(payType.equals(ProductOrderPayTypeEnum.ALI_PAY.name())){
            //支付宝支付
            PayStrategyContext payStrategyContext = new PayStrategyContext(aliPayStrategy);
            return payStrategyContext.executeUnifiedOrder(payInfoVO);
        }
        return "";
    }

    public String closeOrder(PayInfoVO payInfoVO){
        String payType = payInfoVO.getPayType();
        if(payType.equals(ProductOrderPayTypeEnum.WECHAT_PAY.name())){
            //微信支付
            PayStrategyContext payStrategyContext = new PayStrategyContext(wechatPayStrategy);
            return payStrategyContext.executeCloseOrder(payInfoVO);
        }else if(payType.equals(ProductOrderPayTypeEnum.ALI_PAY.name())){
            //支付宝支付
            PayStrategyContext payStrategyContext = new PayStrategyContext(aliPayStrategy);
            return payStrategyContext.executeCloseOrder(payInfoVO);
        }
        return "";
    }

    public String refund(PayInfoVO payInfoVO){
        String payType = payInfoVO.getPayType();
        if(payType.equals(ProductOrderPayTypeEnum.WECHAT_PAY.name())){
            //微信支付
            PayStrategyContext payStrategyContext = new PayStrategyContext(wechatPayStrategy);
            return payStrategyContext.executeRefund(payInfoVO);
        }else if(payType.equals(ProductOrderPayTypeEnum.ALI_PAY.name())){
            //支付宝支付
            PayStrategyContext payStrategyContext = new PayStrategyContext(aliPayStrategy);
            return payStrategyContext.executeRefund(payInfoVO);
        }
        return "";
    }

    public String queryPayStatus(PayInfoVO payInfoVO){
        String payType = payInfoVO.getPayType();
        if(payType.equals(ProductOrderPayTypeEnum.WECHAT_PAY.name())){
            //微信支付
            PayStrategyContext payStrategyContext = new PayStrategyContext(wechatPayStrategy);
            return payStrategyContext.executeQueryPayStatus(payInfoVO);
        }else if(payType.equals(ProductOrderPayTypeEnum.ALI_PAY.name())){
            //支付宝支付
            PayStrategyContext payStrategyContext = new PayStrategyContext(aliPayStrategy);
            return payStrategyContext.executeQueryPayStatus(payInfoVO);
        }
        return "";
    }
}
